
<template>
  <div id="centerInfo">
    <div class="top">
      <ul>
        <li @click="isShow(0)">
          头像
          <img src="../../../wechat/static/images/dzyhzx_next.png" class="right" alt>
          <!-- <img src="../../../wechat/static/images/smxh_ewm.png" class='avator' alt=""> -->
          <img :src="img" class="avator" alt>
          <input class="avator" id='avator' type="file" name="image" accept="image/*" @change="handleSelectPic">
        </li>
        <li @click="isShow(1)">
          名称
          <span>{{name}}</span>
        </li>
        <li @click="isShow(2)">
          手机号
          <span>{{phone}}</span>
        </li>
      </ul>
    </div>
    <div class="middle" @click="logoutbtn">退出登录</div>
    <div class="hide centerInfoHide" v-show="showDialog">
      <div class="mask" @click="showDialog=false"></div>
      <div class="content">
        <div class="one">
          <img @click="showDialog=false" src="../../../wechat/static/images/tc_sc.png" alt> 设置您的{{isName?'名称':'手机号'}}
        </div>
        <div class="two">
          <input type="text" v-model="n" v-show="isName" placeholder="请输入名称">
          <input type="number" v-model="p" v-show="!isName" placeholder="请输入手机号">
          <div>
            <a href="javascript:;" @click="change('1')">确定</a>
            <a href="javascript:;" @click="showDialog=false">取消</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { XDialog } from "vux";
export default {
  name: "centerInfo",
  data() {
    return {
      showDialog: false,
      isName: true,
      img: localStorage.getItem("userImg")||'',
      phone: localStorage.getItem("userPhone")||'',
      name: localStorage.getItem("NuserName")||'',
      n:localStorage.getItem("NuserName")||'',
      p: localStorage.getItem("userPhone")||''
    };
  },
  mounted() {},
  created() {},
  methods: {
    handleSelectPic() {
      var avator = document.getElementById('avator')
      var file = avator.files[0]
      let url =
        this.$CONSTANT.UPLOAD.getOSSUploadUrl +
        "?sessionId=" +
        this.$common.getCookie("sessionId")+'&contentType='+file.type+'&suffix='+file.name.split(".")[1];
      // this.$vux.loading.show({
      //   text: '正在修改头像'
      // })
      this.$common.post(
        url,
        file,
        res => {
          var uploadUrl = res.data.bussData.uploadUrl
          this.$common.uploadFile(uploadUrl, file, file.type, res1 => {
            this.change('img',res.data.bussData)
        })
        }
      );
    },
    change(type,bussData2){
      if(type == '1'){
        if(this.isName){
          if (!this.n) {
            this.$vux.toast.show({
              text: "名字不能为空",
              type: "warn",
                  width: 'auto'
            });
            return;
          }else {
            var params = {
              userId: localStorage.getItem('userId')||'',
              trueName: this.n,
              type: 'normal'
            }
          }
        }else {
          if (!this.p) {
            this.$vux.toast.show({
              text: "手机号不能为空",
              type: "warn",
                  width: 'auto'
            });
            return;
          }else {
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(this.p)) {
              this.$vux.toast.show({
                text: "您的手机号格式不正确，请重新输入",
                type: "warn",
                width: 'auto'
              });
              return;
            }
            var params = {
              userId: localStorage.getItem('userId')||'',
              mobile: this.p
            }
          }
        }
      }else if(type == 'img'){
        var params = {
          userId: localStorage.getItem('userId')||'',
          photoId: bussData2.fileKey
        }
      }
      let url =
        this.$CONSTANT.MY.saveEsShopUserCore +
        "?sessionId=" +
        this.$common.getCookie("sessionId")
      this.$vux.loading.show({
        text: '正在修改数据'
      })
      setTimeout(() => {
        this.$common.post(
          url,
          params,
          res => {
            console.log(res);
            this.$vux.loading.hide()
            var bussData = res.data.bussData
            this.$vux.toast.show({
              text: '修改成功',
              type: "",
              width: 'auto'
            });
            if(type=='img'){
              this.img = bussData2.downloadUrl
              localStorage.setItem("userImg", this.img);
            }else if(type=='1'){
              this.showDialog = false
              if(this.isName){
                this.name = this.n
                localStorage.setItem("NuserName", this.name);
              }else {
                this.phone = this.p
              }
            }
          }
        );
      }, 500);
    },
    logoutbtn() {
      var that = this;
      this.$vux.confirm.show({
        title: "提示",
        content: "是否退出登录",
        onShow() {
          console.log("plugin show");
        },
        onHide() {
          console.log("plugin hide");
        },
        onCancel() {
          console.log("plugin cancel");
        },
        onConfirm() {
          console.log("plugin confirm");

          
          that.logout()
        }
      });
    },
    logout(){
      let url = this.$CONSTANT.MY.normallogout+
        "?sessionId=" +
        this.$common.getCookie("sessionId");;
      this.$vux.loading.show({
        text: "正在退出登录"
      });
      this.$common.post(url, '', res => {
        if (res.status == 200) {
          this.$common.setCookie("sessionId", "");
          this.$common.setCookie("sessionIdnormal", "");
          localStorage.setItem('userImg','')
          
          setTimeout(() => {
            this.$vux.loading.hide();
            this.$vux.toast.show({
              text: "退出成功",
              type: ""
            });
            setTimeout(() => {
              this.$router.replace({
                path:'/phone'
              });
            }, 1000);
          }, 500);
        } else {
          this.$vux.toast.show({
            text: res.msg,
            type: "warn",
            width: 'auto'
          });
        }
      });
    },
    isShow(type) {
      if (type === 0) {
      } else if (type === 1) {
        this.isName = true;
        this.showDialog = true;
      } else {
        return
        this.isName = false;
        this.showDialog = true;
      }
    }
  },
  components: {
    XDialog
  }
};
</script>

<style lang="less">
#centerInfo {
  .top {
    background: #fff;
    border-bottom: 1px solid #efefef;
    ul {
      padding: 0 10px;
      li {
        height: 55px;
        line-height: 55px;
        border-top: 1px solid #efefef;
        font-family: PingFang-SC-Medium;
        font-size: 15px;
        color: #333;
        position: relative;
        span {
          font-family: PingFang-SC-Medium;
          font-size: 15px;
          color: #666;
          float: right;
        }
        &:first-child {
          height: 77px;
          line-height: 77px;
          .avator {
            height: 47px;
            width: 47px;
            float: right;
            border: 1px solid transparent;
            border-radius: 50%;
            position: absolute;
            top: 13px;
            right: 8px;
          }
          input.avator {
            opacity: 0;
          }
          .right {
            float: right;
            width: 5px;
            height: 9px;
            margin-top: 31px;
          }
        }
      }
    }
  }
  .middle {
    border-top: 1px solid #efefef;
    border-bottom: 1px solid #efefef;
    height: 53px;
    line-height: 53px;
    text-align: center;
    background: #fff;
    margin-top: 10px;
  }
}
.centerInfoHide {
  .mask {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background: #000;
    opacity: 0.5;
  }
  .content {
    position: fixed;
    top: 81px;
    width: 269px;
    left: 54px;
    border: 1px solid transparent;
    border-radius: 5px;
    background: #fff;
    .one {
      border-bottom: 1px solid #e2e2e2;
      height: 45px;
      line-height: 45px;
      font-family: PingFang-SC-Medium;
      font-size: 16px;
      color: #333;
      text-align: center;
      // padding-left: 18px;
      img {
        width: 12px;
        height: 12px;
        float: left;
        position: absolute;
        top: 16px;
        left: 18px;
      }
    }
    .two {
      text-align: center;
      input {
        height: 42px;
        line-height: 42px;
        background: #f5f5f5;
        border: 1px solid transparent;
        border-radius: 5px;
        margin: 20px 0 0;
        padding: 0;
        font-family: PingFang-SC-Bold;font-weight: 600;
        font-size: 14px;
        color: #333;
        padding-left: 23px;
      }
      div {
        margin: 48px 0 21px;
        text-align: center;
        a {
          display: inline-block;
          height: 34px;
          width: 108px;
          text-align: center;
          line-height: 34px;
          background: #ee3c4a;
          color: #fff;
          font-size: 13px;
          font-family: PingFang-SC-Bold;font-weight: 600;
          border: 1px solid transparent;
          border-radius: 23px;
          &:last-child {
            background: #ededed;
            margin-left: 8px;
          }
        }
      }
    }
  }
}
</style>
